package stream.concurrent;

import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.stream.Collectors;
import org.testng.annotations.Test;

/**
 * stream结合ComplateableFuture
 * @author zhang peipei
 */
public class ConcurrentWithFuture {

    @Test
    private void t1() {

        var list = List.of(1, 3, 99).stream()
            .map(i -> {
                return CompletableFuture.supplyAsync(() -> {
                    System.out.println(i);
                    return i;
                }).thenApply(res -> res + 1);
            })
            .collect(Collectors.toList());

        // join会阻塞调用这个方法的线程，通常是主线程，所以最好指定某个线程来执行这个方法
        var res = list.stream().map(CompletableFuture::join)
            .collect(Collectors.toList());

        System.out.println(res);

    }

}
